local sheyu = fk.CreateSkill{
    name = "XD__sheyu",
}
Fk:loadTranslationTable{
    ["XD__sheyu"] = "奢欲",
    [":XD__sheyu"] = "回合技，当你需要使用基本牌时，你可以观看一名其他角色的手牌，若其中有你需要使用的牌，其选择一项：令你使用其中一张牌，或失去1点体力。若其以此法进入濒死状态，则其死亡，然后你失去此技能。",
    ["#XD__sheyu"] = "奢欲：观看一名其他角色的手牌，尝试使用其中所需牌",
    ["#XD__sheyu-use"] = "奢欲：观看一名其他角色的手牌，尝试使用其中的【%arg】",
    ["#XD__sheyu1"] = "令%dest使用你手中的一张【%arg】",
    ["#XD__sheyu2"] = "失去1点体力",
    ["#XD__sheyu-choice"] = "奢欲：请选择一项",
    ["#XD__sheyu-hand"] = "奢欲：观看%dest的手牌",
}

sheyu:addEffect("viewas",{
    pattern = ".|.|.|.|.|basic",
    prompt = "#XD__sheyu",
    interaction = function(self, player)
        local all_names = Fk:getAllCardNames("b", true)
        local names = player:getViewAsCardNames(sheyu.name, all_names)
        if #names == 0 then return end
        return UI.CardNameBox {choices = names, all_names = all_names}
    end,
    card_filter = Util.FalseFunc,
    view_as = function(self, player, cards)
        if not self.interaction.data then return end
        local card = Fk:cloneCard(self.interaction.data)
        card.skillName = sheyu.name
        return card
    end,
    before_use = function (self, player, use)
        local room = player.room
        local targets = table.filter(room:getOtherPlayers(player), function (cp)
            return not cp:isKongcheng()
        end)
        local tos = room:askToChoosePlayers(player, {
            max_num = 1,
            min_num = 1,
            targets = targets,
            skill_name = sheyu.name,
            prompt = "#XD__sheyu-use:::"..use.card.trueName,
            cancelable = false,
        })
        if #tos > 0 then
            local to = tos[1]
            local cards = to:getCardIds("h")
            room:viewCards(player, {
                cards = cards,
                skill_name = sheyu.name,
                prompt = "#XD__sheyu-hand::"..to.id
            })
            if table.find(cards, function (cardID)
                return Fk:getCardById(cardID).trueName == use.card.trueName
            end) then
                local choice = room:askToChoice(to, {
                    choices = {"#XD__sheyu1::"..player.id..":"..use.card.trueName,"#XD__sheyu2"},
                    skill_name = sheyu.name,
                    prompt = "#XD__sheyu-choice",
                })
                if choice == "#XD__sheyu2" then
                    room:loseHp(to, 1, sheyu.name)
                    return sheyu.name
                else
                    local subcards = table.filter(cards, function (cardID)
                        return Fk:getCardById(cardID).trueName == use.card.trueName
                    end)
                    if #subcards > 1 then
                        subcards = room:askToChooseCards(player, {
                            target = to,
                            flag = { card_data = {{"$Hand", subcards}} },
                            skill_name = sheyu.name,
                            pattern = use.card.trueName,
                            max = 1,
                            min = 1,
                            prompt = "#XD__sheyu-use:::"..use.card.trueName,
                        })
                    end
                    use.card = Fk:getCardById(subcards[1])
                    --use.card:addSubcards(subcards)
                end
            else
                return sheyu.name
            end
        else
            return sheyu.name
        end
    end,
    enabled_at_play = function (self, player)
        return player:usedSkillTimes(sheyu.name, Player.HistoryTurn) == 0
    end,
    enabled_at_response = function(self, player, response)
        return not response and player:usedSkillTimes(sheyu.name, Player.HistoryTurn) == 0
    end,
})

sheyu:addEffect(fk.EnterDying, {
    can_refresh = function (self, event, target, player, data)
        if not data.damage and not target.dead and target.dying then
            local losehp_event = player.room.logic:getCurrentEvent():findParent(GameEvent.LoseHp)
            return losehp_event and losehp_event.data.who == target and losehp_event.data.skillName == sheyu.name
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        room:killPlayer({ who = target })
        room:handleAddLoseSkills(player, "-XD__sheyu")
    end,
})

return sheyu